<cfcomponent>
	<cfscript>
		this.name = "cfAjaxyDoors";
		this.applicationTimeout = createTimeSpan(0,0,10,0);
		this.clientmanagement= "yes";
		this.loginstorage = "session" ;
		this.sessionmanagement = "yes";
		this.sessiontimeout = createTimeSpan(0,0,10,0);
		this.setClientCookies = "yes";
		this.setDomainCookies = "no";
		this.scriptProtect = "all";
	</cfscript>
   
   <cffunction name="onApplicationStart" output="false">
	 <cfscript>
       application.sessions = 0;
       application.dsn = "requirements";
       application.cfclocation = "#this.name#.cfcs";
       application.title = "cfAjaxyDoors";
       application.subtitle = "Why look through windows when you can go through doors?";
     </cfscript>
     <cftry>
				<!--- Test the database. --->
        <cfquery name="testDb" datasource="#application.dsn#" maxrows="2">
        SELECT COUNT(id)
        FROM myusers
        </cfquery>
        <!--- If error, report an error to the user, log the error information, and do not start the application. --->
        <cfcatch type="database">
			<cfif refindnocase("datasource ([A-Za-z0-9]+) could not be found",cfcatch.message) gt 0>
				Log into the ColdFusion Administrator and create a Data Source named "<cfoutput>#application.dsn#</cfoutput>".
				<cfabort>
			<cfelseif findnocase("error executing database query",cfcatch.message) and refindnocase("table '([A-Za-z0-9.A-Za-z0-9]+)' doesn't exist",cfcatch.detail) gt 0>
				It appears that you have not created the required tables in the database, please do so now.<br>
				<cfabort>
			<cfelse>
				<cfdump var="#cfcatch.message#"><cfdump var="#cfcatch.Detail#">
				<cfabort>
				<cflog 
				file="#this.name#" 
				 type="error"
				text="DB not available. message: #cfcatch.message# Detail: #cfcatch.detail# Native Error: #cfcatch.NativeErrorCode#" >      
				<cfthrow message="This application encountered an error connecting to the database. Please contact support." />
			</cfif>
	        <cfreturn false>
        </cfcatch>
      </cftry>
		<cflog file="#this.name#" type="Information" text="Application #this.name# Started">
      <cfreturn True>
   </cffunction>
   
  <cffunction name="onApplicationEnd" output="false">
	  <cfargument name="applicationScope" required="true">
  </cffunction>
   
  <cffunction name="onSessionStart" output="false">
		<cfscript>
	    session.started = now();
    </cfscript>     
    <cflock scope="application" timeout="5" type="Exclusive">
  	  <cfset application.sessions = application.sessions + 1>
    </cflock>
  </cffunction>
   
	<cffunction name="onSessionEnd" output="false">
    <cfargument name = "sessionScope" required=true/>
    <cfargument name = "applicationScope" required=true/>
    <cfset var sessionLength = TimeFormat(Now() - sessionScope.started, "H:mm:ss")>
    <cflock name="AppLock" timeout="5" type="Exclusive">
        <cfset arguments.applicationScope.sessions = arguments.applicationScope.sessions - 1>
    </cflock>
    <cflog 
      file="#this.name#" 
      type="Information"
      text="Session #arguments.sessionScope.sessionid# ended. Length: #sessionLength# Active sessions: #arguments.applicationScope.sessions#">
	</cffunction>
   
  <cffunction name="onRequestStart">
  </cffunction>
   
   
  <cffunction name="onError" output="true">
    <cfargument name="exception" required=true/>
    <cfargument name="eventName" type="String" required=true/>
    <cfif (trim(arguments.eventName) IS NOT "onSessionEnd") AND (trim(arguments.eventName) IS NOT "onApplicationEnd")>
      <cflog 
      	file="#this.name#" 
        type="error"
      	text="Event name: #arguments.eventName#" >
    	<cflog file="#this.name#" 
      	type="error"
    		text="Message: #arguments.exception.message#">
			<cfoutput>
        <h2>An unexpected error occurred.</h2>
        <p>Please provide the following information to technical support:</p>
        <p>Error Event: #arguments.eventName#</p>
        <p>Error details:</p>
      </cfoutput>   
      <cfdump var="#arguments.exception#">
    <cfelseif (arguments.eventName IS "onApplicationEnd")>
      <cflog 
      	file="#this.name#" 
        type="Information"
      	text="Application #this.name# Ended" >
    </cfif>
  </cffunction>
  
</cfcomponent>